home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / trdwr.c < prev    next >
C/C++ Source or Header  |  1993-03-02  |  3KB  |  146 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "ansidecl.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25.  
  26. #ifndef FILENAME_MAX
  27. #define FILENAME_MAX 256
  28. #endif
  29.  
  30. #ifdef sun
  31. #define remove unlink
  32. #endif
  33.  
  34. extern int errno;
  35.  
  36. int
  37. DEFUN(main, (argc, argv), int argc AND char **argv)
  38. {
  39.   static CONST char hello[] = "Hello, world.\n";
  40.   static CONST char replace[] = "Hewwo, world.\n";
  41.   static CONST size_t replace_from = 2, replace_to = 4;
  42.   char filename[FILENAME_MAX];
  43.   char *name = strrchr(*argv, '/');
  44.   char buf[BUFSIZ];
  45.   FILE *f;
  46.   int lose = 0;
  47.  
  48.   if (name != NULL)
  49.     ++name;
  50.   else
  51.     name = *argv;
  52.  
  53.   (void) sprintf(filename, "test", name);
  54.  
  55. #ifdef atarist
  56.   f = fopen(filename, "wb+");
  57. #else
  58.   f = fopen(filename, "w+");
  59. #endif  
  60.   if (f == NULL)
  61.     {
  62.       perror(filename);
  63.       exit(1);
  64.     }
  65.  
  66.   (void) fputs(hello, f);
  67.   rewind(f);
  68.   (void) fgets(buf, sizeof(buf), f);
  69.   rewind(f);
  70.   (void) fputs(buf, f);
  71.   rewind(f);
  72.   {
  73.     register size_t i;
  74.     for (i = 0; i < replace_from; ++i)
  75.       {
  76.     int c = getc(f);
  77.     if (c == EOF)
  78.       {
  79.         printf("EOF at %u.\n", i);
  80.         lose = 1;
  81.         break;
  82.       }
  83.     else if (c != hello[i])
  84.       {
  85.         printf("Got '%c' instead of '%c' at %u.\n",
  86.            (unsigned char) c, hello[i]);
  87.         lose = 1;
  88.         break;
  89.       }
  90.       }
  91.   }
  92.  
  93.   fseek(f, 0L, SEEK_CUR);
  94.   {
  95.     long int where = ftell(f);
  96.     if (where == replace_from)
  97.       {
  98.     register size_t i;
  99.     for (i = replace_from; i < replace_to; ++i)
  100.       if (putc(replace[i], f) == EOF)
  101.         {
  102.           printf("putc('%c') got %s at %u.\n",
  103.              replace[i], strerror(errno), i);
  104.           lose = 1;
  105.           break;
  106.         }
  107.       }
  108.     else if (where == -1L)
  109.       {
  110.     printf("ftell got %s (should be at %u).\n",
  111.            strerror(errno), replace_from);
  112.     lose = 1;
  113.       }
  114.     else
  115.       {
  116.     printf("ftell returns %u; should be %u.\n", where, replace_from);
  117.     lose = 1;
  118.       }
  119.   }
  120.  
  121.   if (!lose)
  122.     {
  123.       rewind(f);
  124.       if (fgets(buf, sizeof(buf), f) == NULL)
  125.     {
  126.       printf("fgets got %s.\n", strerror(errno));
  127.       lose = 1;
  128.     }
  129.       else if (strcmp(buf, replace))
  130.     {
  131.       printf("Read \"%s\" instead of \"%s\".\n", buf, replace);
  132.       lose = 1;
  133.     }
  134.     }
  135.  
  136.   if (lose)
  137.     printf("Test FAILED!  Losing file is \"%s\".\n", filename);
  138.   else
  139.     {
  140.       (void) remove(filename);
  141.       puts("Test succeeded.");
  142.     }
  143.  
  144.   return(lose ? EXIT_FAILURE : EXIT_SUCCESS);
  145. }
  146.